package com.ycsj.goldmedalnewconcept.teacher.utils;

import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.bean.ClassInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 公共的获取班级列表的请求接口
 * Created by Administrator on 2018/6/28 0028.
 */

public class ClassUtil {

    /**
     * 获取班级列表的请求接口
     *
     * @param rlLoading 加载视图
     */
    public static void httpClass(final View rlLoading, Object object, final onSuccessListener listener) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("ttel", AccountUtils.getAccount(BaseApplication.context));
        map.put("platform", "2");
        map.put("so_id", "1");

        HttpUtils.post(HttpAddress.GETCLASS, map, object, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }
                ToastUtils.showShort(BaseApplication.ERROR);
                Log.i("test", "获取班级数据异常");
            }

            @Override
            public void onResponse(String response, int id) {
                ClassInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, ClassInfo.class);
                } catch (Exception e) {
                    Log.i("test", "数据解析异常");
                }

                if (null != info) {
                    if (1 == info.getState()) {
                        //返回成功
                        List<ClassInfo.ListBean> classInfos = info.getList();
                        if (null != classInfos && classInfos.size() != 0) {
                            //使用回调接口将获取到的班级列表的数据回调出去
                            if (listener != null) {
                                listener.onSuccess(classInfos);
                            }

                        } else {
                            if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                                rlLoading.setVisibility(View.GONE);
                            }
                            ToastUtils.showShort("没有班级数据");
                            if (listener != null) {
                                listener.hasNoData();
                            }
                        }

                    } else {
                        //返回错误
                        if (null != rlLoading && rlLoading.getVisibility() != View.GONE) {
                            rlLoading.setVisibility(View.GONE);
                        }
                        ToastUtils.showShort("服务器返回异常,状态码是:" + info.getState());
                        Log.i("test", "服务器返回异常,状态码是:" + info.getState());
                    }

                } else {
                    Log.i("test", "服务器返回数据为空");
                }

            }
        });
    }

    /**
     * 回调接口
     * 成功获取到班级列表数据后将数据回调出去
     */
    public interface onSuccessListener {
        void onSuccess(List<ClassInfo.ListBean> datas);

        void hasNoData();
    }
}
